import pygame
import random

# 初始化 Pygame
pygame.init()

# 定义常量
WIDTH, HEIGHT = 600, 600
ROWS, COLS = 8, 8
BLOCK_SIZE = WIDTH // COLS
FPS = 60

# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255)]

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("对对碰游戏")
clock = pygame.time.Clock()

# 初始化游戏板
board = [[random.randint(0, len(COLORS) - 1) for _ in range(COLS)] for _ in range(ROWS)]


def draw_board():
    screen.fill(WHITE)
    for row in range(ROWS):
        for col in range(COLS):
            pygame.draw.rect(screen, COLORS[board[row][col]],
                             (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
            pygame.draw.rect(screen, BLACK,
                             (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
    pygame.display.flip()


def check_matches():
    matches = []
    # 检查水平匹配
    for row in range(ROWS):
        for col in range(COLS - 2):
            if board[row][col] == board[row][col + 1] == board[row][col + 2]:
                for i in range(3):
                    if (row, col + i) not in matches:
                        matches.append((row, col + i))
    # 检查垂直匹配
    for col in range(COLS):
        for row in range(ROWS - 2):
            if board[row][col] == board[row + 1][col] == board[row + 2][col]:
                for i in range(3):
                    if (row + i, col) not in matches:
                        matches.append((row + i, col))
    return matches


def remove_matches(matches):
    score = 0
    for row, col in matches:
        board[row][col] = -1
        score += 1
    return score


def fill_empty_spaces():
    for col in range(COLS):
        empty_spaces = []
        for row in range(ROWS - 1, -1, -1):
            if board[row][col] == -1:
                empty_spaces.append(row)
            else:
                if empty_spaces:
                    new_row = empty_spaces.pop(0)
                    board[new_row][col] = board[row][col]
                    board[row][col] = -1
                    empty_spaces.append(row)
        # 填充顶部的空位置
        for row in empty_spaces:
            board[row][col] = random.randint(0, len(COLORS) - 1)


# 游戏主循环
running = True
score = 0
selected = None
while running:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos
            col = x // BLOCK_SIZE
            row = y // BLOCK_SIZE
            if selected is None:
                selected = (row, col)
            else:
                s_row, s_col = selected
                # 检查是否相邻
                if (abs(row - s_row) == 1 and col == s_col) or (abs(col - s_col) == 1 and row == s_row):
                    # 交换方块
                    board[row][col], board[s_row][s_col] = board[s_row][s_col], board[row][col]
                    matches = check_matches()
                    if not matches:
                        # 如果没有匹配，交换回来
                        board[row][col], board[s_row][s_col] = board[s_row][s_col], board[row][col]
                    else:
                        score += remove_matches(matches)
                        fill_empty_spaces()
                selected = None

    draw_board()
    # 显示得分
    font = pygame.font.Font(None, 36)
    text = font.render(f"得分: {score}", True, BLACK)
    screen.blit(text, (10, 10))
    pygame.display.flip()

# 退出游戏
pygame.quit()